package com.tianhui.consignortwofei.myview;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonDialog;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.widget.SelectDialog;
import com.tianhui.consignortwofei.R;

import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;
import org.yczbj.ycrefreshviewlib.item.RecycleViewItemLine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fei
 * @date 2024/1/10
 * @description
 */
public class QuotationSelectDialog <T> extends CommonDialog {
    private int selectPosition;
    private TextView titleTextView;
    private RecyclerView recyclerView;
    private TextView cancelTextView;
    private TextView confirmTextView;
    private QuotationSelectAdapter selectAdapter;
    private String title;
    private SelectDialog.OnSelectItemClickListener onSelectItemClickLister;
    private SelectDialog.OnConfirmClickListener onConfirmClickListener;
    private final List<T> list = new ArrayList<>();

    public QuotationSelectDialog(Context context) {
        super(context, R.style.style_select_dialog);
    }

    public void setList(List<T> list) {
        this.list.clear();
        this.list.addAll(list);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setOnSelectItemClickLister(SelectDialog.OnSelectItemClickListener onSelectItemClickLister) {
        this.onSelectItemClickLister = onSelectItemClickLister;
    }

    public void setOnConfirmClickListener(SelectDialog.OnConfirmClickListener onConfirmClickListener) {
        this.onConfirmClickListener = onConfirmClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select);

        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

        titleTextView = findViewById(R.id.dialog_select_titleTextView);
        recyclerView = findViewById(R.id.dialog_select_recyclerView);
        confirmTextView = findViewById(R.id.dialog_select_confirmTextView);
        cancelTextView = findViewById(R.id.dialog_select_cancelTextView);
        titleTextView.setText(title);
        selectAdapter = new QuotationSelectAdapter(getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(selectAdapter);
        selectAdapter.addAll(list);

        RecycleViewItemLine itemLine = new RecycleViewItemLine(getContext(),
                LinearLayout.HORIZONTAL,
                (int) CommonUtil.convertDpToPixel(1),
                getContext().getResources().getColor(R.color.colorLineGray));
        recyclerView.addItemDecoration(itemLine);

        selectAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                selectPosition = position;
                selectAdapter.setSelectPosition(position);
                selectAdapter.notifyDataSetChanged();

                if (null != onSelectItemClickLister) {
                    onSelectItemClickLister.onItemClick(getClickItem(list.get(selectPosition)), position);
                }
                dismiss();

            }
        });
        confirmTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onConfirmClickListener) {
                    onConfirmClickListener.onConfirmClick(getClickItem(list.get(selectPosition)), selectPosition);
                }
                dismiss();
            }
        });
        cancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private T getClickItem(T item) {
        return item;
    }



    public interface OnSelectItemClickListener<T> {
        void onItemClick(T item, int position);
    }

    public interface OnConfirmClickListener<T> {
        void onConfirmClick(T item, int position);
    }

}

